package com.voxel.sense.infomsg.module.firm.search;

import com.example.com.common.factory.data.DataSource;
import com.example.com.common.utils.NetworkUtils;
import com.example.com.common.utils.ToastUtil;
import com.voxel.sense.infomsg.R;
import com.voxel.sense.infomsg.base.RecyclerViewPresenter;
import cyy.example.com.facotry.model.EventEntity;
import cyy.example.com.facotry.constants.EventBusConstants;
import cyy.example.com.facotry.model.api.BaseEntity;
import cyy.example.com.facotry.model.api.response.FirmListEntity;
import cyy.example.com.facotry.net.helper.FirmNetHelper;
import cyy.example.com.facotry.Factory;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;

import java.util.List;

/**
 * 公司搜索的P层
 * Created by ChenYiYao on 2018/3/7.
 */

public class SearchFirmPresenter extends RecyclerViewPresenter<SearchFirmContract.View >
        implements SearchFirmContract.Presenter, DataSource.Callback<BaseEntity<List<FirmListEntity>>> {
    private String mSearchKey = "";

    @Override
    public void start() {
        super.start();
        EventBus.getDefault().register(this);
    }

    @Override
    public void destroy() {
        super.destroy();
        EventBus.getDefault().unregister(this);
    }

    public SearchFirmPresenter(SearchFirmContract.View view) {
        super(view);
    }

    @Override
    public void loadData() {
        netLoadData(getPAGE_NUMB());
    }

    @Override
    public void loadMoreData() {
        netLoadData(getPAGE_NUMB() );
    }

    @Override
    public void doSearch(String condition) {
        if (NetworkUtils.isConnected()) {
            mSearchKey = condition;
            startRefreshData();
        } else {
            ToastUtil.showToastLong(Factory.app(), Factory.app().getString(R.string.network_unavailable));
            mView.onRefreshFinish();
            mView.showEmptyView(Factory.app().getString(R.string.network_unavailable), true);
        }
    }

    @Override
    public void doLoadMoreData() {
        if (NetworkUtils.isConnected()) {
            startLoadMoreData();
        } else {
            ToastUtil.showToastLong(Factory.app(), Factory.app().getString(R.string.network_unavailable));
            mView.onLoadMoreFinish();
        }
    }

    private void netLoadData(int PAGE_NUMB) {
        FirmNetHelper.searchSpecialFirm(getPageDataSize(), PAGE_NUMB,
                mSearchKey, this);
    }

    @Override
    public void onDataLoaded(BaseEntity<List<FirmListEntity>> data, int reqId) {
        if (mView == null)
            return;
        mView.dimissLoading();
        if (data.isSuccess()) {
            List<FirmListEntity> bean = data.getData();
            //刷新数据
            loadDataComplete(bean);
        } else {
            LoadDataFailure();
        }
    }

    @Override
    public void onDataNotAvailable(String strRes, int reqId) {
        if (mView == null)
            return;
        LoadDataFailure();
        mView.showEmptyView(strRes, true);
    }

    @Subscribe
    public void onEventBus(EventEntity entity) {
        if (mView == null)
            return;
        if (entity != null && entity.key.equals(EventBusConstants.SEARCH_FIRM)) {
            //收到搜索的消息通知
            String mName = (String) entity.value;
            //拉起刷新ui事件
            mView.onUiRefresh(mName);
        }
    }


}
